package YLDataService;

import android.util.Log;

import com.google.gson.Gson;

import org.apache.http.HttpResponse;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.protocol.HTTP;
import org.apache.http.util.EntityUtils;
import org.json.JSONException;
import org.json.JSONObject;

import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.util.ArrayList;
import java.util.List;
import java.util.ListIterator;
import java.util.logging.Handler;

import TaskClass.User;

/**
 * Created by Administrator on 2015/1/20.
 */
public class GetAndPost {

    private  String URL = "http://192.168.200.137:8055";

    public void httpPost(final String Method ){//通过Http  Post请求
        new Thread(new Runnable() {
            @Override
            public void run() {   //post请求是保密性好,参数不应该在地址栏显示
                List<User> userList = new ArrayList<User>();
                String url = URL+"/"+Method;
                HttpPost post=new HttpPost(url);
                try {
                    //添加数值到User类
                    Gson gson =new Gson();

                    //设置POST请求中的参数
                    JSONObject p = new JSONObject();
                    post.setEntity(new StringEntity(p.toString()));//将参数设置入POST请求
                    p.put("",gson.toJson(userList));

                    post.setHeader(HTTP.CONTENT_TYPE, "text/json");//设置为json格式。

                    HttpClient client=new DefaultHttpClient();
                    HttpResponse response=client.execute(post);
                    if(response.getStatusLine().getStatusCode()==200){
                        String content= EntityUtils.toString(response.getEntity());    //得到返回字符串
                        Log.d("WCF", content);//打印到logcat
                        //analysisJSON(content);
                    }
                } catch (UnsupportedEncodingException e) {
                    e.printStackTrace();
                } catch (ClientProtocolException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                } catch (IOException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();}

                catch (Exception e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }

            }
        }).start();
    }


    /*
    public void analysisJSON(String content) {//解析JSON
        Gson gson = new Gson();
        u = gson.fromJson(content, new TypeToken<List<User>>() {
        }.getType());
        for (User user : u) {
            Log.d("WCF", user.EmpID + user.Name);
            data.add(user.EmpID);// user.EmpID加入到data，显示到listview里
        }
        mh.sendEmptyMessage(0);
    }

    Handler mh = new Handler() {   //以Handler为桥梁将结果List<String> data 传入UI
        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);

        }
    };

*/
}
